<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="班级" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="classId">
              <j-dict-select-tag type="list" v-model="model.classId" @change="getTeacher" dictCode="sk_class,class_name,id,del_flag = 0 order by sort_no asc" placeholder="请选择班级" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="recordDate">
              <j-date placeholder="请选择日期" value-format="YYYY-MM-DD" v-model="model.recordDate"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="餐次" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="mealNumber">
              <j-dict-select-tag type="list" v-model="model.mealNumber" dictCode="meals_type" placeholder="请选择餐次" />
              <!-- <a-input v-model="model.mealNumber" placeholder="请输入餐次"  ></a-input> -->
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="陪餐老师" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="accompanyTeacher">
              <a-select v-model="model.accompanyTeacher" placeholder="请输入陪餐老师">
                <a-select-option v-for="item in teacherList" :key="item.id" :value="item.id">
                  {{ item.realname }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="陪餐照片" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="accompanyImage">
              <j-image-upload :number="imgMaxNumber" isMultiple  v-model="model.accompanyImage" ></j-image-upload>
              <div>最多上传 <span style="color: #f5222d;">{{ imgMaxNumber }}</span> 张陪餐照片</div>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="饭菜品种评价" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="typeEstimate">
              <a-textarea v-model="model.typeEstimate" rows="4" placeholder="请输入饭菜品种评价" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="饭菜搭配评价" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="collocationEstimate">
              <a-textarea v-model="model.collocationEstimate" rows="4" placeholder="请输入饭菜搭配评价" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="饭菜数量评价" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="amountEstimate">
              <a-textarea v-model="model.amountEstimate" rows="4" placeholder="请输入饭菜数量评价" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="儿童就餐表现" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="showEstimate">
              <a-textarea v-model="model.showEstimate" rows="4" placeholder="请输入儿童就餐表现" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="食堂卫生评价" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sanitationEstimate">
              <a-textarea v-model="model.sanitationEstimate" rows="4" placeholder="请输入食堂卫生评价" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="保育工作评价" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="workEstimate">
              <a-textarea v-model="model.workEstimate" rows="4" placeholder="请输入保育工作评价" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="其他意见建议" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="otherOpinion">
              <a-textarea v-model="model.otherOpinion" rows="4" placeholder="请输入其他意见建议" />
            </a-form-model-item>
          </a-col>
          
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'TbAccompanyMealTableForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        imgMaxNumber:9,
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          classId: [
            { required: true, message: '请选择班级!'},
          ],
          recordDate: [
            { required: true, message: '请选择日期!'},
          ],
          mealNumber: [
            { required: true, message: '请选择餐次!'},
          ],
          accompanyTeacher: [
            { required: true, message: '请选择陪餐老师!'},
          ],
          accompanyImage: [
            { required: true, message: '请选择陪餐照片!'},
          ],
        },
        url: {
          add: "/table/tbAccompanyMealTable/add",
          edit: "/table/tbAccompanyMealTable/edit",
          queryById: "/table/tbAccompanyMealTable/queryById"
        },
        teacherList:[]
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
      // this.getTeacher();
    },
    methods: {
      getTeacher(){
        let url = "/management/teacher/list"
        const params = {
          classCodes:this.model.classId
        }
        getAction(url,params).then((res)=>{
          if(res.success){
            this.teacherList = res.result;
          } else {
            this.$message.warning(res.message);
          }
        }).catch((error)=>{
          console.error(error);
        })
      },
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            this.$emit("modelLoading",that.confirmLoading)
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              this.$emit("modelLoading",that.confirmLoading)
            })
          }
         
        })
      },
    }
  }
</script>